package com.example.p2pinvest.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author h2tc01
 * @version 1.0.0
 * 2022/8/10
 * 抽取公共部分,自定义BaseAdapter
 */
public abstract class MyBaseAdapter<T> extends BaseAdapter {

    //要展示的数据集合
    public List<T> list = new ArrayList<>();

    public MyBaseAdapter(List<T> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //将视图的初始化  视图的关联  数据的展示  全部交给BaseHolder的子类去完成

        BaseHolder<T> holder;
        if (convertView == null){
            holder = getHolder();
        }else {
            holder = (BaseHolder<T>) convertView.getTag();
        }

        //装配数据
        T t = list.get(position);
        //装配数据并展示到视图
        holder.setData(t);

        return holder.getRootView();
    }

    protected abstract BaseHolder<T> getHolder();

}
